package servlet;

import model.student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/s_selectClasses")
public class s_selectClasses extends HttpServlet{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setCharacterEncoding("UTF-8");

        String[] s=req.getParameterValues("choices");

        String useAccount=req.getParameter("username");

        if(s!=null){
            System.out.println(useAccount+"选择的课程有:");
            for (String temp:s) {
                System.out.println(temp);
                int num=student.addClassByAccount(useAccount,Integer.parseInt(temp));
                if(num==0){
                    System.out.println("选课失败");
                    req.setAttribute("selectInfo",temp+"选课失败！");
                    req.getRequestDispatcher("/s_selectClassesShow").forward(req,resp);
                }
                else {
                    req.setAttribute("selectInfo",temp+"选课成功！");
                    req.getRequestDispatcher("/s_selectClassesShow").forward(req,resp);
                }
            }
        }
        else{
            req.setAttribute("selectInfo","你还没有选课！");
            req.getRequestDispatcher("/jsp/s_selectClasses.jsp?username="+useAccount).forward(req,resp);
        }
    }
}
